package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.entity.notifications;
import com.example.springboot.service.notificationsService;
import com.example.springboot.mapper.notificationsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

import java.util.List;

@CrossOrigin(origins = "http://localhost:7070")
@RestController
@RequestMapping("/notifications")
public class notificationsController {
    @Autowired
    private notificationsService notificationsService;

    @Autowired
    private notificationsMapper notificationsMapper;

    @GetMapping("/notifications")
    public Result getAllnotifications(@RequestParam int user_id) {
        try {
            List<notifications> notifications = notificationsService.findnotificationsByUserId(user_id);
            return Result.success(notifications);
        } catch (Exception e) {
            return Result.error("500", "获取通知列表失败");
        }
    }

    @PostMapping("/markAsRead")
    public Result markAsRead(@RequestBody notifications notifications) {
        try {
            System.out.println("Received request to mark as read for notification_id: " + notifications.getNotification_id());
            notificationsService.markAsRead(notifications.getNotification_id());
            String readStatus = notificationsService.getReadStatus(notifications.getNotification_id());
            System.out.println("Read status after update: " + readStatus);
            return Result.success("通知已标记为已读");
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈信息，便于调试
            return Result.error("500", "标记通知为已读失败");
        }
    }

    // 其他通知相关的API，如创建、更新和删除通知
}